package com.example.myapplication.hilt

import android.app.Application
import android.util.Log
import com.example.myapplication.uniapp.TestModule
import com.orhanobut.logger.AndroidLogAdapter
import com.orhanobut.logger.FormatStrategy
import com.orhanobut.logger.Logger
import com.orhanobut.logger.PrettyFormatStrategy
import com.taobao.weex.WXSDKEngine
import dagger.hilt.android.HiltAndroidApp
import io.dcloud.feature.sdk.DCSDKInitConfig
import io.dcloud.feature.sdk.DCUniMPSDK
import io.dcloud.feature.sdk.DCUniMPSDK.IDCUNIMPPreInitCallback
import io.dcloud.feature.sdk.MenuActionSheetItem
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.embedding.engine.FlutterEngineCache
import io.flutter.embedding.engine.dart.DartExecutor
import io.reactivex.functions.Consumer
import io.reactivex.plugins.RxJavaPlugins

/**
 * 类描述:
 * 创建人:   tangchao$
 * 创建时间: 2021/8/8$ 4:57 PM$
 * 联系方式: 419704299@qq.com
 * 修改人:   tangchao$
 * 修改时间: 2021/8/8$ 4:57 PM$
 * 修改备注:  [说明本次修改内容]
 */
@HiltAndroidApp
class TestHiltApplication : Application() {
    lateinit var flutterEngine: FlutterEngine
    override fun onCreate() {
        super.onCreate()
        //取消订阅后，抛出的异常无法捕获，导致程序崩溃
        RxJavaPlugins.setErrorHandler(Consumer {
            Logger.d("TestHiltApplication===onCreate: " + it.message);

        })
        OpenLogMethod()
        flutterEngine = FlutterEngine(this)
        flutterEngine.getNavigationChannel().setInitialRoute("/one_page");
        flutterEngine.dartExecutor.executeDartEntrypoint(
            DartExecutor.DartEntrypoint.createDefault()
        )
        FlutterEngineCache.getInstance().put("my_engine_id", flutterEngine)

        WXSDKEngine.registerModule("TestModule", TestModule::class.java)

        //初始化uniapp
        inituniapp()
    }

    private fun inituniapp() {
        val item = MenuActionSheetItem("关于", "gy")
        val sheetItems: MutableList<MenuActionSheetItem> = ArrayList()
        sheetItems.add(item)
        val config: DCSDKInitConfig = DCSDKInitConfig.Builder()
            .setCapsule(true)
            .setMenuDefFontSize("16px")
            .setMenuDefFontColor("#ff00ff")
            .setMenuDefFontWeight("normal")
            .setMenuActionSheetItems(sheetItems)
            .build()
        DCUniMPSDK.getInstance().initialize(
            this, config
        ) { b ->
            Logger.d("TestHiltApplication===inituniapp: SDK是否初始化成功="+b);
        }
    }

    /**
     * 方法描述: 开启日志记录方法
     * 创建人:  tangchao
     * 创建时间: 2017/10/23 12:37
     * 联系方式:419704299@qq.com
     */
    private fun OpenLogMethod() {
        val formatStrategy: FormatStrategy = PrettyFormatStrategy.newBuilder()
            .showThreadInfo(true) // (Optional) Whether to show thread info or not. Default true
            .methodCount(0) // (Optional) How many method line to show. Default 2
            .methodOffset(3) // (Optional) Skips some method invokes in stack trace. Default 5
            //        .logStrategy(customLog) // (Optional) Changes the log strategy to print out. Default LogCat
            .tag("coroutines") // (Optional) Custom tag for each log. Default PRETTY_LOGGER
            .build()
        Logger.addLogAdapter(AndroidLogAdapter(formatStrategy))
    }
}
